package org.freespace.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.freespace.model.Machine;
import org.freespace.service.IMachineService;
import org.freespace.utils.SessionUtil;
import org.freespace.utils.enums.MachineStatus;
import org.freespace.utils.response.CommonResult;
import org.freespace.vo.MachineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 压测机器表 前端控制器
 * </p>
 *
 * @author liuqi
 * @since 2023-05-08
 */
@Slf4j
@RestController
@RequestMapping("/machine")
public class MachineController {

    @Autowired
    IMachineService machineService;


    @PostMapping(value = "list")
    public CommonResult list(@RequestBody MachineVO vo) {
        Page<Machine> page = new Page<>(vo.getCurrentPage(), vo.getPageSize());
        Wrapper<Machine> wrapper = Wrappers.<Machine>emptyWrapper();
        if(StringUtils.isNotBlank(vo.getCondition())) {
            wrapper = Wrappers.<Machine>lambdaQuery()
                    .like(Machine::getId, vo.getCondition())
                    .or().like(Machine::getIp, vo.getCondition())
                    .or().like(Machine::getCreateBy, vo.getCondition());
        }
        IPage<Machine> data = machineService.page(page, wrapper);
        for (Machine record : data.getRecords()) {
            record.setStatusName(MachineStatus.getNameByType(record.getStatus()));
        }
        return CommonResult.success(data);
    }

    @PostMapping("save")
    public CommonResult save(@RequestBody Machine machine) throws IOException {
        machine.setCreateBy(SessionUtil.getCurrentUser());
        machine.setCreateTime(LocalDateTime.now());
        machine.setLastUpdateBy(SessionUtil.getCurrentUser());
        machine.setLastUpdateTime(LocalDateTime.now());
        machineService.save(machine);
        return CommonResult.success();
    }

    @GetMapping("delete/{id}")
    public CommonResult save(@PathVariable("id") int id) {
        machineService.removeById(id);
        return CommonResult.success();
    }


    @GetMapping("upgrade/{id}")
    public CommonResult upgrade(@PathVariable("id") int id) throws Exception{
        machineService.upgrade(id);
        return CommonResult.success();
    }

    @GetMapping("restart/{id}")
    public CommonResult restart(@PathVariable("id") int id) throws Exception{
        machineService.restart(id);
        return CommonResult.success();
    }

    @PostMapping("callback")
    public CommonResult callback(@RequestBody Map<String, String> map) throws Exception {
        log.info("callback:{}", map);
        String ip = map.get("ip");
        String jmeter = map.get("jmeter");

        if ("0".equals(jmeter)) {
            machineService.updateMachineStatus(ip, MachineStatus.FAIL.getType(), map);
        } else {
            machineService.updateMachineStatus(ip, MachineStatus.IDLE.getType(), map);
        }
        return CommonResult.success();
    }
}
